package com.one.consumer.order.ui;

import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.one.common.common.order.AddMaterialItem;
import com.one.common.common.order.MaterialStateItem;
import com.one.common.common.order.YouhuiquanBean;
import com.one.common.common.order.binder.MaterialInfoBinder;
import com.one.common.common.order.binder.MaterialStateBinder;
import com.one.common.common.order.binder.YouhuijuanBinder;
import com.one.common.config.MyContact;
import com.one.common.config.OrderState;
import com.one.common.config.RouterPath;
import com.one.common.manager.RouterManager;
import com.one.common.model.extra.DefaultExtra;
import com.one.common.utils.SystemUtils;
import com.one.common.view.pagestate.listpage.BaseListActivity;
import com.one.common.view.widget.MyTitleBar;
import com.one.consumer.R;
import com.one.consumer.common.state.OrderStateHandle;
import com.one.consumer.order.model.item.OrderDetailItem;
import com.one.consumer.order.presenter.OrderDetailPresenter;
import com.one.consumer.order.ui.binder.OrderDetailBinder;
import com.one.consumer.user.model.extra.InvoiceExtra;

/**
 * author ：zhulunjun
 * email  ：zhulunjun@qq.com
 * date   ：2018/8/8 下午10:28
 * description ：订单详情
 **/
@Route(path = RouterPath.ORDER_DETAIL)
public class OrderDetailActivity extends BaseListActivity<OrderDetailPresenter> implements OrderDetailView {
    private TextView btn1, btn2, btn3, btn4, btn5;
    private OrderDetailItem orderDetailItem;

    @Override
    protected void initPresenter() {
        mPresenter = new OrderDetailPresenter(this, this);
    }

    @Override
    protected void initTitle() {
        super.initTitle();
        getMyTitleBar()
                .setMode(MyTitleBar.Mode.BACK_TITLE)
                .setTitleText("订单详情");
    }

    @Override
    public void registerDate() {
        register(OrderDetailItem.class, new OrderDetailBinder());
        register(MaterialStateItem.class, new MaterialStateBinder());
        register(AddMaterialItem.class, new MaterialInfoBinder());
//        register(YouhuiquanBean.class, new YouhuijuanBinder());
    }

    @Override
    public void loadData() {
        mPresenter.getOrderDetail();
    }

    @Override
    public void initView() {
        super.initView();
        hindLoadMore();
        hindRefresh();
        addBottomView(R.layout.layout_order_detail_bottom_btn);
        btn1 = successView.findViewById(R.id.tv_btn_1);
        btn2 = successView.findViewById(R.id.tv_btn_2);
        btn3 = successView.findViewById(R.id.tv_btn_3);
        btn4 = successView.findViewById(R.id.tv_btn_4);
        btn5 = successView.findViewById(R.id.tv_btn_5);

        btn1.setOnClickListener(this);
        btn2.setOnClickListener(this);
        btn3.setOnClickListener(this);
        btn4.setOnClickListener(this);
        btn5.setOnClickListener(this);

    }

    @Override
    protected void onResume() {
        super.onResume();
        doRefresh();
    }

    @Override
    public void setOrderDetail(OrderDetailItem item) {
        if (item != null) {
            orderDetailItem = item;
            OrderStateHandle.setStateViewDetail(item, btn1, btn2, btn3, btn4, btn5);
            if(item.getOrder_state().equals(OrderState.WAIT_CONFIRM)){
                // 滚动到底部
                getListView().scrollToPosition(getAdapter().getItemCount()-1);
            }
        }
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.tv_btn_1: // 取消订单
                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setMessage("确定取消订单？")
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                mPresenter.cancelOrder(orderDetailItem.getOrder_id() + "");
                                dialog.dismiss();
                            }
                        })
                        .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                dialog.dismiss();
                            }
                        }).show();

                break;
            case R.id.tv_btn_2: // 删除订单
                AlertDialog.Builder builder1 = new AlertDialog.Builder(this);
                builder1.setMessage("确定删除订单？")
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                mPresenter.deleteOrder(orderDetailItem.getOrder_id() + "");
                                dialog.dismiss();
                            }
                        })
                        .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                dialog.dismiss();
                            }
                        }).show();


                break;
            case R.id.tv_btn_3: // 联系客服
                SystemUtils.call(this, MyContact.PHONE_KEFU);
                break;
            case R.id.tv_btn_4: // 联系师傅， 立即确认， 立即支付，索要发票
                switch (orderDetailItem.getOrder_state()) {
                    case OrderState.WAIT_VISIT: // 待上门 联系师傅
                        if (orderDetailItem.getMemberWorkerBean() != null) {
                            SystemUtils.call(this, orderDetailItem.getMemberWorkerBean().getMember_account());
                        }
                        break;
                    case OrderState.WAIT_CONFIRM: // 等待确认服务 立即确认
                        AlertDialog.Builder builder2 = new AlertDialog.Builder(mContext);
                        builder2.setMessage("您确定已知维修报价并需要维修？");
                        builder2.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                mPresenter.confirmOrder(orderDetailItem.getOrder_id() + "");
                            }
                        });

                        builder2.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {

                            }
                        });
                        builder2.show();
                        break;
                    case OrderState.WAIT_PAY: // 待付款 立即支付
                        AlertDialog.Builder builder3 = new AlertDialog.Builder(mContext);
                        builder3.setMessage("您确定已知维修报价并需要维修？");
                        builder3.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                mPresenter.payOrder(orderDetailItem.getOrder_id() + "");
                            }
                        });

                        builder3.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {

                            }
                        });

                        builder3.show();


                        break;
                    case OrderState.PAY_END: // 已付款 索要发票
                        // 跳转页面
                        InvoiceExtra extra = new InvoiceExtra(InvoiceExtra.CHECK);
                        extra.setMoney(orderDetailItem.getOrder_price());
                        extra.setOrderId(orderDetailItem.getOrder_id() + "");
                        RouterManager.getInstance().go(RouterPath.INVOICE_MANAGER, extra);
                        break;
                }
                break;
            case R.id.tv_btn_5: // 服务评价
                if (orderDetailItem.getAssessment_state().equals("1")) {
                    // 跳转页面
                    RouterManager.getInstance().go(RouterPath.ADD_COMMENT,
                            new DefaultExtra(orderDetailItem.getOrder_id() + ""));
                }

                break;
            default:
                break;
        }
    }
}
